package com.example.selfcontrol.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.selfcontrol.config.Response;
import com.example.selfcontrol.dto.UserInfoDTO;
import com.example.selfcontrol.entity.*;
import com.example.selfcontrol.entity.resp.UserResponse;
import com.example.selfcontrol.mapper.ArticleMapper;
import com.example.selfcontrol.mapper.FollowerMapper;
import com.example.selfcontrol.mapper.UserMapper;
import com.example.selfcontrol.service.ArticleService;
import com.example.selfcontrol.service.LikeOptService;
import com.example.selfcontrol.service.StarOptService;
import com.example.selfcontrol.service.UserService;
import com.example.selfcontrol.utils.JwtUtils;
import com.example.selfcontrol.utils.UserExpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private  UserMapper userMapper ;

    @Autowired
    private ArticleMapper articleMapper ;

    @Autowired
    private FollowerMapper followerMapper ;

    @Override
    public Response addUser(User user) {
        String name = user.getName();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        User query = userMapper.selectOne(queryWrapper.eq("name",name)) ;
        System.out.println(query);

        if(query != null) {
            System.out.println("存在!");
            return Response.fail();
        }

        userMapper.insert(user);
        return Response.ok();
    }

    @Override
    public Response loginUser(Credit credit) {
        String creditName = credit.getName() ;
        String creditPassword = credit.getPassword() ;

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        User query = userMapper.selectOne(queryWrapper.eq("name",creditName)) ;

        if(creditPassword.equals(query.getPassword())) {
            String token = JwtUtils.generateToken(query);
            System.out.println(token);
            return new Response(true,"200",UserResponse.createFromUser(query,token));
        }

        return Response.fail();
    }
    @Override
    public User getUser(Integer id) {
        User user = userMapper.selectById(id) ;
        return user ;
    }

    @Override
    public Response updateUser(User user,String token) {
        // 先判断这个用户是否在非法篡改
        if(JwtUtils.isUserRight(token,user)) {
            try {
                userMapper.updateById(user) ;
            } catch (Exception e) {
                e.printStackTrace();
                return Response.fail() ;
            }
            return Response.ok() ;
        }
        else return new Response(false,"401","非法操作") ;
    }


    @Override
    public UserInfoDTO getUserInfo(int userId) {
        int subscribeCount = followerMapper.selectCount(new QueryWrapper<Follower>().eq("follower_id",userId)) ;
        int followerCount = followerMapper.selectCount(new QueryWrapper<Follower>().eq("followee_id",userId)) ;
        int articleCount = articleMapper.selectCount(new QueryWrapper<Article>().eq("author_id",userId)) ;

        return new UserInfoDTO(subscribeCount,followerCount,articleCount) ;
    }


    @Override
    public Response IncUserExp(int userId,int incCount) {
        User user = userMapper.selectById(userId) ;
        int increase = user.getExp() + incCount ;
        user.setExp(increase);
        userMapper.updateById(user) ;

        return Response.ok() ;
    }

    @Override
    public String UserRank(int userId) {
        User user = userMapper.selectById(userId) ;
        int Exp = user.getExp() ;

        return UserExpUtils.JudgeLevel(Exp) ;
    }


    @Autowired
    private StarOptService starOptService ;

    @Autowired
    private LikeOptService likeOptService ;

    @Autowired
    private ArticleService articleService ;

    @Override
    public Response UpdateUserArticleNumber(Long userId) {
        User user = userMapper.selectById(userId) ;

        int articleNumber = articleService.CountPersonalArticles(userId) ;
        user.setArticleCount(articleNumber);
        try {
            userMapper.updateById(user) ;
        }catch (Exception e) {
            return Response.fail() ;
        }

        return Response.ok() ;
    }
}
